package cn.ztuo.bitrade.controller.ieo;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.CommonStatus;
import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.controller.BaseController;
import cn.ztuo.bitrade.entity.Admin;
import cn.ztuo.bitrade.entity.Coin;
import cn.ztuo.bitrade.entity.IeoOdf;
import cn.ztuo.bitrade.entity.IeoOdfrecord;
import cn.ztuo.bitrade.service.CoinService;
import cn.ztuo.bitrade.service.IeoOdfService;
import cn.ztuo.bitrade.service.IeoOdfrecordService;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.vo.EmptionRecrodVO;
import cn.ztuo.bitrade.vo.IeoEmptionVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @Description:
 * @Author: fiwork.net
 * @Date: 2019/4/26 4:02 PM
 */
@RestController
@RequestMapping("ieo")
@Slf4j
public class ieoTwoController extends BaseController {

    @Autowired
    private IeoOdfService ieoEmptionService;

    @Autowired
    private IeoOdfrecordService emptionRecordService;

    @Autowired
    private CoinService coinService;

    /**
     * 分页查询IEO
     * @param ieoEmptionVO
     * @return
     */
    @RequiresPermissions("ieo:page-query")
    @RequestMapping(value = "page-query",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.IEO, operation = "分页查找IEO")
    public MessageResult getByPage(IeoEmptionVO ieoEmptionVO){
        try {
        log.info("------分页查询ieo------"+ JSONObject.toJSONString(ieoEmptionVO));
        Page<IeoOdf> result = ieoEmptionService.getByPage(ieoEmptionVO);
        return success(result);
        } catch (Exception e) {
            log.info("--------分页查询ieo异常={}",e);
        }
        return error("获取失败");
    }
    /**
     * 保存/修改IEO
     //* @param admin
     * @param ieoEmption
     * @return
     */
    @RequiresPermissions("ieo:save")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.IEO, operation = "保存/修改IEO")
    public MessageResult saveIEO(
            @SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin,
            IeoOdf ieoEmption){
        log.info("------保存/修改IEO------"+ JSONObject.toJSONString(ieoEmption));
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        if(ieoEmption.getId() == null){
            if(ieoEmption.getType()!=4) return error(400,"只能创建用户认购类型");
            Coin coin = coinService.findOne(ieoEmption.getCoin().toUpperCase());
            if(coin == null){
                return error(400,"发售币种不存在");
            }else {
                if(coin.getStatus().equals(CommonStatus.ILLEGAL)) return error(400,"发售币种被禁用");
            }
           IeoOdf result = ieoEmptionService.save(ieoEmption);
           if(result.getId()>0) {
               return success("创建成功");
           }else{
               return error(400,"创建失败");
           }
        }
        ieoEmptionService.save(ieoEmption);
        return success("修改成功");
    }

    /**
     * 获取IEO详情
     * @param id
     * @return
     */
    @RequiresPermissions("ieo:detail")
    @RequestMapping(value = "detail",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.IEO,operation = "获取IEO详情")
    public Object detail(@RequestParam(value = "id") Long id){
        IeoOdf ieoEmption = ieoEmptionService.findById(id);
        Assert.notNull(ieoEmption,"validate ieo_id!");
        return success(ieoEmption);
    }
    /**
     * 根据ID删除IEO
     * @return
     */
    //    @RequiresPermissions("ieo:del")
    //    @RequestMapping(value = "del/{id}",method = RequestMethod.GET)
    //    @AccessLog(module = AdminModule.IEO, operation = "删除IEO")
    //    public MessageResult delIEO(@PathVariable("id")Long id){
    //        IeoOdf ieoEmption = ieoEmptionService.findById(id);
    //        if(ieoEmption == null) return error(400,"ID不存在");
    //        int status = ieoEmption.getStatus();
    //        if(status == 2) {
    //            ieoEmption.setStatus(3);
    //            ieoEmptionService.save(ieoEmption);
    //            return success("删除成功");
    //        }else{
    //            return error(400,"只能删除完成计划");
    //        }
    //    }
    /**
     * 分页查询用户IEO认购记录
     * @param emptionRecrodVO
     * @return
     */
    @RequiresPermissions("ieo:record-page")
    @RequestMapping(value = "record-page",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.IEO, operation = "分页查找IEO认购记录")
    public MessageResult getRecordByPage(EmptionRecrodVO emptionRecrodVO){
        log.info("------分页查找IEO认购记录------"+ JSONObject.toJSONString(emptionRecrodVO));
        try {
            Page<IeoOdfrecord> result = emptionRecordService.getByPage(emptionRecrodVO);
            return success(result);
        } catch (Exception e) {
            log.info("--------分页查找IEO认购记录异常={}",e);
        }
        return error("获取失败");
    }
}
